package at.fhooe.mcm.logic.logging;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;


import at.fhooe.mcm.logic.core.Core;

public class HTMLLoggingService extends LoggingService
{
	File			m_file;
	BufferedWriter	m_out;

	Logger			m_logger;
	
	public static final String	NAME = "logging.html";
	public static final String LOGGING_SOURCE = "HTMLLoggingService";

	
	public static HTMLLoggingService getInstance(Core _core)
	{
		return (HTMLLoggingService) _core.getService(NAME);
	}

	public HTMLLoggingService(int _level)
	{
		m_level = _level;
	}

	public HTMLLoggingService()
	{
		this(WARNING);
	}


	@Override
	public String getName()
	{
		return NAME;
	}

	@Override
	public void initialize(Core _core)
	{
		m_core = _core;
		
		try
		{
			
			DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 
			
			String filename = "log_" + dateFormat.format(m_core.getTime()) + ".html";
			File f = new File(filename);
			
			if (!f.exists())
			{
				f.createNewFile();
			}

			m_out = new BufferedWriter(new FileWriter(f));

		}
		catch (IOException _ioe)
		{
			ConsoleLoggingService.getInstance(m_core).logWarning(LOGGING_SOURCE, _ioe.getMessage());
		}

		initializeHTML();
	}
	
	public void logSensorData(String _id, String _type, String _name, String _value)
	{
		try
		{
			m_out.write("<tr>\n");
			m_out.write("<td>");
			m_out.write(timeToString(m_core.getTime()));
			m_out.write("</td>\n");
			
			m_out.write("<td>");
			m_out.write(_id);
			m_out.write("</td>\n");
			
			m_out.write("<td>");
			m_out.write(_type);
			m_out.write("</td>\n");
			
			m_out.write("<td>");
			m_out.write(_name);
			m_out.write("</td>\n");
			
			m_out.write("<td>");
			m_out.write(_value);
			m_out.write("</td>\n");
			m_out.write("</tr>\n");
			m_out.flush();
		}
		catch (IOException _ioe)
		{
			ConsoleLoggingService.getInstance(m_core).logWarning(LOGGING_SOURCE, _ioe.getMessage());
		}
	}
	
	@Override
	protected void log(int _level, String source, String message)
	{

	}

	@Override
	public void shutdown(Core core)
	{
		try
		{
			closeHTML();
			m_out.close();
		}
		catch (IOException e)
		{

		}
	}
	
	private void initializeHTML()
	{
		try
		{
			m_out.write("<html>\n");
			m_out.write("<body>\n");
			m_out.write("<div align = \"center\"");
			m_out.write("<table border=\"1\" width=\"80%\">\n");
			
			m_out.write("<tr>\n");
			m_out.write("<td width=\"10%\">");
			m_out.write("Time");
			m_out.write("</td>\n");
			
			m_out.write("<td width=\"10%\">");
			m_out.write("ID");
			m_out.write("</td>\n");
			
			m_out.write("<td width=\"20%\">");
			m_out.write("Type");
			m_out.write("</td>\n");
			
			m_out.write("<td width=\"30%\">");
			m_out.write("Name");
			m_out.write("</td>\n");
			
			m_out.write("<td width=\"30%\">");
			m_out.write("Value");
			m_out.write("</td>\n");
			m_out.write("</tr>\n");
			m_out.flush();
		}
		catch (IOException _ioe)
		{
			ConsoleLoggingService.getInstance(m_core).logWarning(LOGGING_SOURCE, _ioe.getMessage());
		}
	}

	private void closeHTML()
	{
		try
		{
			m_out.write("</table>\n");
			m_out.write("</div>\n");
			m_out.write("</body>\n");
			m_out.write("</html>\n");
			m_out.flush();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
